package zhuyun.code

fun main(args:Array<String>){
    val or = Originator()
    val cr = Caretaker()
    or.state = "S0"
    println("初始状态:${or.state}")
    cr.memento = or.createMemento()
    or.state = "S1"
    println("新的状态:${or.state}")
    or.restoreMemento(cr.memento)
    println("恢复状态:${or.state}")

}
class Memento(val state:String)
class Caretaker{
    lateinit var memento:Memento
}
class Originator{
    var state:String? =null
    fun createMemento():Memento{
        return Memento(state!!)
    }
    fun restoreMemento(m:Memento){
        state = m.state
    }
}